import axios from 'axios';
import router from '../router/index';
import { Message } from 'element-ui';
import $HttpCodeUtil from './HttpCodeUtil';

// axios.defaults.baseURL = process.env.NODE_ENV === 'development' ? '/api' : 'http://122.112.244.178:8080';
// axios.defaults.baseURL = process.env.BASE_API;
axios.defaults.baseURL = "http://106.14.206.172:1159";
//axios.defaults.baseURL = "http://172.16.4.249:8080"

axios.defaults.timeout = 30000;
// axios.defaults.withCredentials = true;
axios.defaults.headers = {
    'Content-Type': 'application/json;charset=utf-8',
};

// 创建axios请求实例
const request = axios.create();

// 添加请求拦截器
request.interceptors.request.use(
    function(config) {
        // 在发送请求之前处理
        config.headers['auth-token'] = window.localStorage.token;
        return config;
    },
    function(error) {
        // 发送请求错误处理
        console.info(error);
        return Promise.reject(error);
    }
);

// 添加响应拦截器
request.interceptors.response.use(
    function(response) {
        if (response.data.code == $HttpCodeUtil.CODE_AUTHC_FALSE || response.data.code == $HttpCodeUtil.CODE_AUTHC_EXPIRED) {
            Message.error(response.data.desc);
            router.push({ name: 'MgtLogin' });
            return;
        }

        switch (response.data.code) {
            case 40101:
            case 40102:
                Message.error(response.data.desc);
                router.push({ name: 'MgtLogin' });
                break;
            case 40201:
                var actived = window.location.hash.substr(
                    window.location.hash.indexOf("menu=")
                ).replace("menu=", "");
                router.push({ name: 'MgtIdx401', query: { menu: actived } });
                break;
            default:
                return Promise.resolve(response.data);
        }
    },
    function(error) {
        if (error.response.status == 404) {
            Message.error(error.response.data);
            router.push({ name: 'MgtIdx404' });
        } else {
            Message.error(error.response.data);
        }
        return Promise.reject(error.response);
    }
);

const $HttpUtil = {
    axios: axios,
    get: function(url, params) {
        return new Promise(function(resolve, reject) {
            request({
                    url: url,
                    method: 'get',
                    params: params,
                })
                .then(function(result) {
                    if (result) {
                        resolve(result);
                    }
                })
                .catch(function(error) {
                    reject(error);
                });
        });
    },
    post: function(url, datas) {
        return new Promise(function(resolve, reject) {
            request({
                    url: url,
                    method: 'post',
                    data: datas,
                })
                .then(function(result) {
                    if (result) {
                        resolve(result);
                    }
                })
                .catch(function(error) {
                    reject(error);
                });
        });
    },
};

export default $HttpUtil;